<?php

require("Dao.php");
require("./view/DisplayUIInv.php");

class DaoInventory extends Dao {  

	private $Display;
	
    public function DaoInventory($action) {	
	
		$this->Display = new DisplayUIInv();			
        
		switch ($action) {
            case "view":
                $this->viewInventory();
                break;
            case "add":
                $this->addInventoryItem();				
                break;
            case "submit":
                $this->submitInventoryItem($_POST['item_name'],$_POST['item_qty']); 
				$this->viewInventory();				
                break; 
			case "edit":
				$this->editInventoryItem();
				$this->viewInventory();
				break;
			case "edit name":
				$this->viewEditInv("name");
				break;
			case "edit qty":
				$this->viewEditInv("qty");
				break;
			case "delete":
				//$this->deleteInventoryItem();
				if( $_POST['item_id'] == "" ) {
					$this->viewDelInv();
				}
				else
					$this->deleteInventoryItem( $_POST['item_id'] );
					$this->viewInventory();
				break;
			default:
				$this->viewInventory();
        } 		
    }

	public function viewInventory() {
	 	$this->Display->displayInv($this->pharseQ("SELECT * FROM inventory"));
    }
	


    public function addInventoryItem() {       
    	$this->Display->displayAddItemForm();
    }

    public function submitInventoryItem($iname,$iqty) {    
		if( $iname == "" ) {
			echo "<h3>No item name</h3>";
			$this->addInventoryItem();
		}
		else if ( $iqty == "" ) {
			echo "<h3>No item qty</h3>";
			$this->addInventoryItem();
		}
		else {
			$this->pharseQ( "INSERT INTO inventory (`item_id`, `item_name`, `item_qty`) 
							 VALUES (NULL, '" . $iname . "', '" . $iqty . "')");
			echo "<h3 style=\"color:#060\">Item Submitted</h3>\n";
		}
    }
	
	public function viewEditInv($mod) {
		$this->Display->displayEditInv($this->pharseQ("SELECT * FROM inventory"),$mod);
	}

    public function editInventoryItem() {
	//UPDATE `ran49406_scms`.`inventory` SET `item_qty` = '10' WHERE `inventory`.`item_id` =37 LIMIT 1 ;
		$id = $_POST['item_id'];
		$name = $_POST['item_name'];
		$qty = $_POST['item_qty'];
		
		if ( $qty != "" ) {
			$this->pharseQ( "UPDATE inventory SET `item_qty` = '" . $qty . "' WHERE `item_id` =" . $id . " LIMIT 1 ;" );
			echo "<h3 style=\"color:#060\">Item QTY Modified</h3>\n";
		}
		else if ( $name != "" ) {
			$this->pharseQ( "UPDATE inventory SET `item_name` = '" . $name . "' WHERE `item_id` =" . $id . " LIMIT 1 ;" );
			echo "<h3 style=\"color:#060\">Item NAME Modified</h3>\n";
		}
    }
	
	public function viewDelInv() {
		$this->Display->displayDeletionInv($this->pharseQ("SELECT * FROM inventory"));
	}


    public function deleteInventoryItem($iId) {
		$this->pharseQ("DELETE FROM inventory WHERE `item_id` = " . $iId );
		echo "<h3 style=\"color:#060\">Item Deleted</h3>\n";
    }
}

?>
